﻿Imports System.Data.SqlClient

Public Class DuToan
    Private sMaLoaiDt As String
    Private sSoDt As String
    Private dNgayLap As Date
    Private sSoTc As String
    Private sMaDv As String

    Public Property MaLoaiDt() As String
        Get
            Return sMaLoaiDt
        End Get
        Set(ByVal Value As String)
            sMaLoaiDt = Value
        End Set
    End Property

    Public Property SoDt() As String
        Get
            Return sSoDt
        End Get
        Set(ByVal Value As String)
            sSoDt = Value
        End Set
    End Property

    Public Property NgayLap() As Date
        Get
            Return dNgayLap
        End Get
        Set(ByVal Value As Date)
            dNgayLap = Value
        End Set
    End Property

    Public Property SoTc() As String
        Get
            Return sSoTc
        End Get
        Set(ByVal Value As String)
            sSoTc = Value
        End Set
    End Property

    Public Property MaDv() As String
        Get
            Return sMaDv
        End Get
        Set(ByVal Value As String)
            sMaDv = Value
        End Set
    End Property

End Class

Public Class DuToanF

    Public Function Get_List_DonVi() As DataTable
        Dim myProjCnn As New ProjectConnection
        Dim myCnn As New SqlConnection
        Dim myDat As New SqlDataAdapter
        Dim myDtb As New DataTable

        myCnn = myProjCnn.GetConnection
        myCnn.Open()

        Dim myCmd As New SqlCommand
        With myCmd
            .Connection = myCnn
            .CommandType = CommandType.StoredProcedure
            .CommandText = "DuToan_Get_List_DonVi"

            .Parameters.Add(New SqlClient.SqlParameter("@nam", SqlDbType.Int))
            .Parameters("@nam").Value = Project_Nam

        End With

        myDat = New SqlDataAdapter(myCmd)
        myDat.Fill(myDtb)
        Return myDtb
        myCnn.Close()
    End Function

    Public Function Get_Last_SoDt() As DataTable
        Dim myProjCnn As New ProjectConnection
        Dim myCnn As New SqlConnection
        Dim myDat As New SqlDataAdapter
        Dim myDtb As New DataTable

        myCnn = myProjCnn.GetConnection
        myCnn.Open()

        Dim myCmd As New SqlCommand
        With myCmd
            .Connection = myCnn
            .CommandType = CommandType.StoredProcedure
            .CommandText = "DuToan_Get_Last_SoDt"

            .Parameters.Add(New SqlClient.SqlParameter("@nam", SqlDbType.Int))
            .Parameters("@nam").Value = Project_Nam

        End With

        myDat = New SqlDataAdapter(myCmd)
        myDat.Fill(myDtb)
        Return myDtb
        myCnn.Close()
    End Function

    Public Function Get_List_ThiCong(ByVal MaDv As String, ByVal MaLoaiDt As String, ByVal SoDt As String) As DataTable
        Dim myProjCnn As New ProjectConnection
        Dim myCnn As New SqlConnection
        Dim myDat As New SqlDataAdapter
        Dim myDtb As New DataTable

        myCnn = myProjCnn.GetConnection
        myCnn.Open()

        Dim myCmd As New SqlCommand
        With myCmd
            .Connection = myCnn
            .CommandType = CommandType.StoredProcedure
            .CommandText = "DuToan_Get_List_ThiCong"

            .Parameters.Add(New SqlClient.SqlParameter("@madv", SqlDbType.Char))
            .Parameters("@madv").Value = MaDv

            .Parameters.Add(New SqlClient.SqlParameter("@maloaidt", SqlDbType.Char))
            .Parameters("@maloaidt").Value = MaLoaiDt

            .Parameters.Add(New SqlClient.SqlParameter("@sodt", SqlDbType.Char))
            .Parameters("@sodt").Value = SoDt

        End With

        myDat = New SqlDataAdapter(myCmd)
        myDat.Fill(myDtb)
        Return myDtb
        myCnn.Close()
    End Function

    Public Function Get_SoDt(ByVal SoDt As String) As DataTable
        Dim myProjCnn As New ProjectConnection
        Dim myCnn As New SqlConnection
        Dim myDat As New SqlDataAdapter
        Dim myDtb As New DataTable

        myCnn = myProjCnn.GetConnection
        myCnn.Open()

        Dim myCmd As New SqlCommand
        With myCmd
            .Connection = myCnn
            .CommandType = CommandType.StoredProcedure
            .CommandText = "DuToan_Get_SoDt"

            .Parameters.Add(New SqlClient.SqlParameter("@sodt", SqlDbType.Char))
            .Parameters("@sodt").Value = SoDt

        End With

        myDat = New SqlDataAdapter(myCmd)
        myDat.Fill(myDtb)
        Return myDtb
        myCnn.Close()
    End Function

    Public Function Get_Filter(ByVal MaDv As String) As DataTable
        Dim myProjCnn As New ProjectConnection
        Dim myCnn As New SqlConnection
        Dim myDat As New SqlDataAdapter
        Dim myDtb As New DataTable

        myCnn = myProjCnn.GetConnection
        myCnn.Open()

        Dim myCmd As New SqlCommand
        With myCmd
            .Connection = myCnn
            .CommandType = CommandType.StoredProcedure
            .CommandText = "DuToan_Get_Filter"

            .Parameters.Add(New SqlClient.SqlParameter("@madv", SqlDbType.Char))
            .Parameters("@madv").Value = MaDv

            .Parameters.Add(New SqlClient.SqlParameter("@nam", SqlDbType.Int))
            .Parameters("@nam").Value = Project_Nam

        End With

        myDat = New SqlDataAdapter(myCmd)
        myDat.Fill(myDtb)
        Return myDtb
        myCnn.Close()
    End Function

    Public Function Exists_SoDt(ByVal SoDt As String) As Boolean
        Dim myProjCnn As New ProjectConnection
        Dim myCnn As New SqlConnection
        Dim myDat As New SqlDataAdapter
        Dim myDtb As New DataTable

        myCnn = myProjCnn.GetConnection
        myCnn.Open()

        Dim myCmd As New SqlCommand
        With myCmd
            .Connection = myCnn
            .CommandType = CommandType.StoredProcedure
            .CommandText = "DuToan_Exists_SoDt"

            .Parameters.Add(New SqlClient.SqlParameter("@sodt", SqlDbType.Char))
            .Parameters("@sodt").Value = SoDt

            .Parameters.Add(New SqlClient.SqlParameter("@return", SqlDbType.Bit)).Direction = ParameterDirection.ReturnValue
            .ExecuteNonQuery()
        End With

        If myCmd.Parameters("@return").Value = 1 Then
            myCnn.Close()
            myCnn.Dispose()
            Return True
        Else
            myCnn.Close()
            myCnn.Dispose()
            Return False
        End If
    End Function

    Public Function Save(ByVal DuToan As DuToan) As Boolean
        Dim myProjCnn As New ProjectConnection
        Dim myCnn As New SqlConnection
        Dim myDat As New SqlDataAdapter
        Dim myDtb As New DataTable

        myCnn = myProjCnn.GetConnection
        myCnn.Open()

        Dim myCmd As New SqlCommand
        With myCmd
            .Connection = myCnn
            .CommandType = CommandType.StoredProcedure
            .CommandText = "DuToan_Save"

            .Parameters.Add(New SqlClient.SqlParameter("@maloaidt", SqlDbType.Char))
            .Parameters("@maloaidt").Value = DuToan.MaLoaiDt

            .Parameters.Add(New SqlClient.SqlParameter("@sodt", SqlDbType.Char))
            .Parameters("@sodt").Value = DuToan.SoDt

            .Parameters.Add(New SqlClient.SqlParameter("@ngaylap", SqlDbType.SmallDateTime))
            .Parameters("@ngaylap").Value = DuToan.NgayLap

            .Parameters.Add(New SqlClient.SqlParameter("@madv", SqlDbType.Char))
            .Parameters("@madv").Value = DuToan.MaDv

            .Parameters.Add(New SqlClient.SqlParameter("@sotc", SqlDbType.Char))
            .Parameters("@sotc").Value = DuToan.SoTc

            .Parameters.Add(New SqlClient.SqlParameter("@return", SqlDbType.Bit)).Direction = ParameterDirection.ReturnValue
            .ExecuteNonQuery()
        End With

        If myCmd.Parameters("@return").Value = 1 Then
            myCnn.Close()
            myCnn.Dispose()
            Return True
        Else
            myCnn.Close()
            myCnn.Dispose()
            Return False
        End If
    End Function

    Public Function Delete(ByVal SoDt As String) As Boolean
        Dim myProjCnn As New ProjectConnection
        Dim myCnn As New SqlConnection
        Dim myDat As New SqlDataAdapter
        Dim myDtb As New DataTable

        myCnn = myProjCnn.GetConnection
        myCnn.Open()

        Dim myCmd As New SqlCommand
        With myCmd
            .Connection = myCnn
            .CommandType = CommandType.StoredProcedure
            .CommandText = "DuToan_Delete"

            .Parameters.Add(New SqlClient.SqlParameter("@sodt", SqlDbType.Char))
            .Parameters("@sodt").Value = SoDt

            .Parameters.Add(New SqlClient.SqlParameter("@return", SqlDbType.Bit)).Direction = ParameterDirection.ReturnValue
            .ExecuteNonQuery()
        End With

        If myCmd.Parameters("@return").Value = 1 Then
            myCnn.Close()
            myCnn.Dispose()
            Return True
        Else
            myCnn.Close()
            myCnn.Dispose()
            Return False
        End If
    End Function

End Class

